import LogisticsApi from "@/api/logistics";
import {
  EquipmentInfo,
  AnnouncementInfo,
  UserManagementInfo,
} from "@/api/logistics/model";

export const LogisticsStore = defineStore("logistics", () => {
  const equipment = ref(<Array<EquipmentInfo>>[
    {
      id: 0,
      name: "",
      location: "",
      status: "available",
      createdAt: new Date(),
    },
  ]);
  const announcement = ref(<Array<AnnouncementInfo>>[
    {
      id: 0,
      title: "",
      category: "event",
      content: "",
      createdAt: new Date(),
    },
  ]);
  const userManagement = ref(<Array<UserManagementInfo>>[
    {
      id: 0,
      blacklist: 0,
      userName: 0,
      blacklistDays: 0,
    },
  ]);
  // 获取信息
  function getEquipmentInfo() {
    return new Promise<EquipmentInfo>((resolve, reject) => {
      LogisticsApi.getEquipments()
        .then((res: any) => {
          const data = res.data;
          if (!data) {
            console.log("Verification failed, please Login again.");
            reject("Verification failed, please Login again.");
            return;
          }
          Object.assign(equipment.value, { ...data });
          resolve(data);
        })
        .catch((error) => {
          console.log(error);
          reject(error);
        });
    });
  }
  function getAnnouncementInfo() {
    return new Promise<AnnouncementInfo>((resolve, reject) => {
      LogisticsApi.getAnnouncements()
        .then((res: any) => {
          const data = res.data;
          if (!data) {
            console.log("Verification failed, please Login again.");
            reject("Verification failed, please Login again.");
            return;
          }
          Object.assign(announcement.value, { ...data });
          resolve(data);
        })
        .catch((error) => {
          console.log(error);
          reject(error);
        });
    });
  }
  function getUserManagementInfo() {
    return new Promise<UserManagementInfo>((resolve, reject) => {
      LogisticsApi.getUserManagements()
        .then((res: any) => {
          const data = res.data;
          if (!data) {
            console.log("Verification failed, please Login again.");
            reject("Verification failed, please Login again.");
            return;
          }
          Object.assign(userManagement.value, { ...data });
          resolve(data);
        })
        .catch((error) => {
          console.log(error);
          reject(error);
        });
    });
  }
  return {
    userManagement,
    announcement,
    equipment,
    getEquipmentInfo,
    getAnnouncementInfo,
    getUserManagementInfo,
  };
});
